home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Periodicals / develop / develop 4 code / Poly. in Code Resources / Virtual WDEF / VirtualWorld.cp < prev    next >
Encoding:
Text File  |  1990-07-24  |  2.1 KB  |  100 lines  |  [TEXT/MPS ]

  1. /*
  2.     VirtualWorld.cp
  3.     
  4.     Implementation of Virtual world maintenance class.
  5.     
  6.     This class maintains an A5 global space, as well as supporting
  7.     initialization of virtual function tables at run-time.
  8.     
  9.     by Patrick Beard.
  10.     
  11.     based on TN 256.
  12.  
  13.     ©1990 by Patrick C. Beard.  All rights reserved.
  14.  */
  15.  
  16. #ifndef __VIRTUALWORLD__
  17. #include "VirtualWorld.h"
  18. #endif
  19.  
  20. #ifndef __QUICKDRAW__
  21. #include <QuickDraw.h>
  22. #endif
  23.  
  24. extern "C" {
  25. void A5Init(Ptr);        // function that sets up (initializes) the A5 world.
  26. long A5Size(void);        // function that returns the size of our A5 world.
  27. void init_vtbls(void);    // function that initializes virtual tables.
  28. }
  29.  
  30. // utility routine to switch A5's.  (lifted from OSUtils.h)
  31. pascal Ptr SwapA5(Ptr newA5)
  32.     = {0x2F4D,0x0004,0x2A5F};
  33.  
  34. // constructor:  set up the current App's required A5 world.
  35.  
  36. VirtualWorld::VirtualWorld(Boolean worldFloats)
  37. {
  38.     MoveHigh();
  39.     Lock();
  40.     
  41.     error = noErr;                                // default is no error.
  42.     codeFloats = worldFloats;
  43.     oldA5 = nil;                                // none established for them.
  44.     worldSize = A5Size();                        // store our size for speed.
  45.     ourA5 = NewPtr(worldSize);                    // better not fail.
  46.     if(!ourA5) {
  47.         error = -1;
  48.         return;
  49.     }
  50.     
  51.     // initialize our globals.
  52.     A5Init(ourA5 + worldSize - 32);                // see TN 256.
  53.     
  54.     // call InitGraf to initialize quickdraw globals.
  55.     GrafPtr aPort;
  56.     GetPort(&aPort);
  57.     Enter();                                    // go inside our world.
  58.     InitGraf(&qd.thePort);
  59.     SetPort(aPort);
  60.     
  61.     // if this code doesn't float, only call this function once.
  62.     if(!codeFloats)
  63.         init_vtbls();
  64.         
  65.     Leave();                                    // leave the virtual world.
  66.     
  67.     Unlock();
  68. }
  69.  
  70. VirtualWorld::~VirtualWorld()
  71. {
  72.     Leave();                                    // restore any saved A5.
  73.     if(ourA5)
  74.         DisposPtr(ourA5);
  75. }
  76.  
  77. void VirtualWorld::Enter()
  78. {
  79.     GrafPtr currPort;
  80.     GetPort(&currPort);
  81.  
  82.     oldA5 = SwapA5(ourA5 + (worldSize - 32));
  83.     
  84.     SetPort(currPort);
  85.  
  86.     if(codeFloats) {
  87.         // call virtual table initialization functions everytime our world is
  88.         // entered.  this way, if we've been relocated, the tables will be correct.
  89.         init_vtbls();
  90.     }
  91. }
  92.  
  93. void VirtualWorld::Leave()
  94. {
  95.     if(oldA5) {
  96.         SwapA5(oldA5);
  97.         oldA5 = nil;                            // clear it so we won't do it again.
  98.     }
  99. }
  100.